/**
 * @Title:  AttentionDynamicsActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2015-12-7 上午10:20:14 
 * @version:  V1.0 
 */

package artid.com.arttogo.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.ArtistFollow;
import artid.com.arttogo.bean.ArtistInfo;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.CircleImageView;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author 熊彬
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */
public class CollecterIntroActivity extends BaseFragment implements OnClickListener {
	public  static String artistName,artisUrl,artistImg;
	private View mView;
	ImageView mImgHead,auth,chatBtn;
	TextView mTextName,mTextRec,//备案作品
	mTextFollow,//藏家关注
	ArtistInfo,mTextMainIntro;
	private ImageLoader mImageLoader;
	Boolean isVer;//是否鉴证
	String id = "";
	View gz,qxgz;
	int gznum = 0;
	ArtistInfo mArtistInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Bundle args = getArguments();
		id = args != null ? args.getString("id") : "";
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mView = inflater.inflate(R.layout.activity_collecter_intro, null);
		mImgHead = (CircleImageView)mView.findViewById(R.id.icon_artcenter);
		mTextName = (TextView)mView.findViewById(R.id.artinfo_name);
		mTextRec = (TextView)mView.findViewById(R.id.artinfo_cp);
		mTextFollow = (TextView)mView.findViewById(R.id.artinfo_dz);
		mTextMainIntro = (TextView)mView.findViewById(R.id.collecter_intro_textmain);
		mTextMainIntro.setMovementMethod(ScrollingMovementMethod.getInstance());
		gz = (View)mView.findViewById(R.id.artdet_gz);
		qxgz = (View)mView.findViewById(R.id.artdet_qxgz);
		auth = (ImageView)mView.findViewById(R.id.artist_authicon);
		chatBtn = (ImageView)mView.findViewById(R.id.artdet_chat);
		chatBtn.setOnClickListener(this);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		reqData("");
	}

	void reqData(final String keywork) {

		VolleyBasic.getInstance(getActivity()).post(
				Cache.artistInfo,
				VolleyMap.getInstance().artistInfo(id), ArtistInfo.class,
				new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub

						artid.com.arttogo.bean.ArtistInfo item = (ArtistInfo) vd.getData();
						mArtistInfo=(ArtistInfo) vd.getData();
						mTextName.setText(item.getRealname());
						artistName = item.getRealname();
						artistImg  = item.getImgurl();
						artisUrl = item.getShare();
						mTextRec.setText(item.getArt_num()+"件作品");
						mTextFollow.setText(item.getFollow_num()+"人关注");
						if (item.getImgurl().contains("http")) {
							RequestQueue mQueue = Volley.newRequestQueue(getActivity());
							mImageLoader = new ImageLoader(mQueue, new BitmapCache());
							com.android.volley.toolbox.ImageLoader.ImageListener listener = ImageLoader
									.getImageListener(mImgHead,
											R.drawable.ic_menu_rotate,
											R.drawable.ic_menu_rotate);
							mImageLoader.get(item.getImgurl(), listener);
						}
						try {
							gznum = Integer.valueOf(item.getFollow_num());
						} catch (Exception e) {
							// TODO: handle exception
						}
						String isfollow = item.getIs_follow();
						gz.setOnClickListener(listenerGz(item.getId(), true));
						qxgz.setOnClickListener(listenerGz(item.getId(), false));
						if (isfollow.equals("1")) {
							qxgz.setVisibility(View.VISIBLE);
							gz.setVisibility(View.GONE);
						}else {
							qxgz.setVisibility(View.GONE);
							gz.setVisibility(View.VISIBLE);
						}
						if (item.getIs_auth().equals("1")) {
							auth.setVisibility(View.VISIBLE);

						}else {
							auth.setVisibility(View.GONE);
						}
						if (!TextUtils.isEmpty(item.getMemo())) {
							mTextMainIntro.setText(item.getMemo());
						}


					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub

					}
				});
	}

	View.OnClickListener listenerGz(final String id, final Boolean setFollow){
		return new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!AppUtil.LoginVer(getActivity())) {
					reqData(id,setFollow);
				}
			}
		};
	}

	void reqData(String id, final Boolean setFollow){
		String state = "";
		if (setFollow) {
			state = "1";
		}else {
			state = "2";
		}
		VolleyBasic.getInstance(getActivity()).post(Cache.artistFollow, 
				VolleyMap.getInstance().artistFollow(AppUtil.getUser().getToken(), id, state), ArtistFollow.class,
				new VolleyCallback() {

			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				ArtistFollow itemArtistFollow  = (ArtistFollow)vd.getData();

				if (itemArtistFollow.getState().equals("1")) {
					if (setFollow) {
						gznum++;
						gz.setVisibility(View.GONE);
						qxgz.setVisibility(View.VISIBLE);
					}else {
						gznum--;
						gz.setVisibility(View.VISIBLE);
						qxgz.setVisibility(View.GONE);
					}

				}
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub

			}
		});
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.artdet_chat:
			//聊天
			if(mArtistInfo!=null){
				AppUtil.goChat(getActivity(), mArtistInfo.getUid(), mArtistInfo.getImgurl(), mArtistInfo.getRealname());
			}
			break;

		default:
			break;
		}
	}
}
